package com.yusong.algorithm.array;


/**
 * 双向指针
 */
public class TwoPoint167 {



    public static void main(String[] args) {
        String str = "A man, a plan, a canal: Panama";
        TwoPoint167 main = new TwoPoint167();
        main.isPalindrome(str);
    }

    public int[] twoSum(int[] numbers, int target) {
        int length = numbers.length;

        int right = 0;
        int left = length-1;

        while (right < left){
            int sum = numbers[right] + numbers[left];
            if(sum == target){
                break;
            }
            if(sum < target){
                right++;
            }else {
                left--;
            }
        }

        int[] result = new int[2];
        result[0]= right+1;
        result[1]= left+1;

        return result;
    }

    /**
     * 回文串
     */
    public boolean isPalindrome(String s) {
        if(null == s){
            return true;
        }
        char[] numbers = s.toLowerCase().toCharArray();
        int length = numbers.length;

        int right = 0;
        int left = length-1;



        while (right < left){
            char rightValue = numbers[right];
            if(!Character.isLetterOrDigit(rightValue)){
                right++;
                continue;
            }

            char leftValue = numbers[left];
            if(!Character.isLetterOrDigit(leftValue)){
                left--;
                continue;
            }

            if(rightValue != numbers[left]){
                return false;
            }else {
                right++;
                left--;
            }
        }
        return true;
    }


}
